<?php
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );
class Monitor extends CI_Controller {
	public function index() {
		redirect ( "monitor/monitor_index" );
	}
	public function monitor_index() {
		$this->load->model ( 'region_model' );
		$data ['regions'] = $this->region_model->query_region_list ( 0 );
		$this->load->model ( "media_model" );
		$data ['statistics'] = $this->media_model->query_statistics ();
		$this->load->model ( "monitor_model" );
		$data ['industry_list'] = $this->monitor_model->query_industry ( 0 );
		$data ['title'] = "监测数据搜索搜索";
		$data ['search_data'] = null;
		$this->load->view ( 'monitor/monitor_header', $data );
		$this->load->view ( 'common/menu' );
		$this->load->view ( 'monitor/monitor' );
		$this->load->view ( 'common/footer' );
	}
	public function search_brand() {
		$this->load->model ( "monitor_model" );
		$keyword = trim ( $this->input->get ( "keyword" ) );
		$result ['result'] = $this->monitor_model->search_brand ( $keyword );
		if (count ( $result ['result'] ) < 10) {
			$to_search_product_num = 10 - count ( $result ['result'] );
			$product_list = $this->monitor_model->search_product ( $keyword, $to_search_product_num );
			$result ['result'] = array_merge ( $result ['result'], $product_list );
		}
		echo json_encode ( $result );
	}
	public function search_industry_category() {
		$industry_id = $this->input->get ( "industry_id" );
		$this->load->model ( "monitor_model" );
		$industry_list = $this->monitor_model->query_industry ( $industry_id );
		if ($industry_list == null) {
			$industry_list = array (
					array (
							"industry_id" => $industry_id,
							"industry_name" => " - " 
					) 
			);
		}
		echo json_encode ( $industry_list );
	}
	public function search_monitor() {
		$this->load->helper ( "common" );
		$data ['media_type'] = $this->input->get ( "media_type" );
		$data ['brand_name'] = $this->input->get ( "brand_name" );
		$data ['province'] = $this->input->get ( "province" );
		$data ['city'] = $this->input->get ( "city" );
		$data ['area'] = $this->input->get ( "area" );
		$data ['highway'] = $this->input->get ( "highway" );
		$data ['airport'] = $this->input->get ( "airport" );
		$data ['start_date'] = $this->input->get ( "start_date" );
		$data ['end_date'] = $this->input->get ( "end_date" );
		$data ['industry'] = $this->input->get ( "industry" );
		$data ['industry_category'] = $this->input->get ( "industry_category" );
		$data ['search_type'] = $this->input->get ( "search_type" );
		$data ['show_type'] = $this->input->get ( "show_type" );
		$data ['remove_duplicate'] = $this->input->get ( "remove_duplicate" );
		$data ['brand_id'] = $this->input->get ( "brand_id" );
		$data ['search_name_type'] = $this->input->get ( "search_name_type" );
		if ($data ['media_type'] == false) {
			$data ['media_type'] = "common";
		}
		if ($data ['show_type'] != "monitor_search_list") {
			$data ['show_type'] = "monitor_search_grid";
		}
		// 分页大小
		$start = intval ( $this->input->get ( "per_page" ) );
		$limit = intval ( $this->input->get ( "page_size" ) );
		if ($limit < 1) {
			$limit = 20;
		}
		$data ['page_size'] = $limit;
		// 定义搜索条件
		$data ['search_data'] = $data;
		
		// 媒体信息
		$this->load->model ( 'monitor_model' );
		$search_result = $this->monitor_model->search_monitor ( $data ['search_data'], $start, $limit );
		$data ['monitor_list'] = $search_result ['list'];
		$data ['num'] = $search_result ['total'];
		$data ['industry_list'] = $this->monitor_model->query_industry ( 0 );
		$data ['industry_category_list'] = array ();
		if ($data ['industry'] != "") {
			$data ['industry_category_list'] = $this->monitor_model->query_industry ( $data ['industry'] );
			if ($data ['industry_category_list'] == null) {
				$data ['industry_category_list'] = array (
						array (
								"industry_id" => $data ['industry'],
								"industry_name" => "-" 
						) 
				);
			}
		}
		
		// 分页
		$this->load->library ( 'common/my_page' );
		$url = '?' . http_build_query ( $data ['search_data'] );
		$data ['list_top_page_link'] = $this->my_page->get_pagination ( $url, $data ['num'], $limit, false );
		$data ['bottom_page_link'] = $this->my_page->get_pagination ( $url, $data ['num'], $limit );
		
		// 区域
		$this->load->model ( 'region_model' );
		$data ['regions'] = $this->region_model->query_region_list ( 0 );
		$data ['citys'] = array ();
		if ($data ['province'] != "") {
			$data ['citys'] = $this->region_model->query_region_list ( $data ['province'] );
		}
		$data ['areas'] = array ();
		if ($data ['city'] != "") {
			$data ['areas'] = $this->region_model->query_region_list ( $data ['city'] );
		}
		$data ['highways'] = array ();
		if ($data ['media_type'] == "highway") {
			$data ['highways'] = $this->region_model->get_highways ( $data ['province'] );
		}
		$data ['airports'] = array ();
		if ($data ['media_type'] != "airport") {
			$data ['airports'] = $this->region_model->get_airports ( $data ['province'] );
		}
		
		// 统计数据
		$this->load->model ( "media_model" );
		$data ['statistics'] = $this->media_model->query_statistics ();
		$data ['title'] = "媒体监测搜索";
		$this->load->view ( 'monitor/monitor_header', $data );
		$this->load->view ( 'common/menu' );
		$this->load->view ( 'monitor/monitor_search_grid' );
		$this->load->view ( 'common/footer' );
	}
}